<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace Lg\Support;

/**
 * Description of Validator
 *
 * @author getssl
 */
class Validator {

    /**
     * 
     * @param string $ip
     * @param string $which  ipv4 or ipv6
     * @return bool
     */
    public static function isValidIP(string $ip, string $which = null) {
        switch (strtolower($which)) {
            case 'ipv4':
                $which = FILTER_FLAG_IPV4;
                break;
            case 'ipv6':
                $which = FILTER_FLAG_IPV6;
                break;
            default:
                $which = NULL;
                break;
        }
        return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
    }

    /**
     * 验证手机号码
     */
    public static function isMobile($str) {
        if (empty($str)) {
            return false;
        }

        return preg_match('#^13[\d]{9}$|14^[0-9]\d{8}|^15[0-9]\d{8}$|^18[0-9]\d{8}$#',
                $str);
    }

    /**
     * 验证固定电话
     */
    public static function isTel($str) {
        if (empty($str)) {
            return true;
        }
        return preg_match('/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/',
                trim($str));
    }

    /**
     * 验证qq号码
     */
    public static function isQQ($str) {
        if (empty($str)) {
            return false;
        }

        return preg_match('/^[1-9]\d{4,12}$/', trim($str));
    }

    /**
     * 验证邮政编码
     */
    public static function isZipCode($str) {
        if (empty($str)) {
            return true;
        }

        return preg_match('/^[1-9]\d{5}$/', trim($str));
    }

    /**
     * 验证ip
     */
    public static function isIP($value) {
        return filter_var($value, \FILTER_VALIDATE_IP) !== false;
    }

    /**
     * 验证身份证(中国)
     */
    public static function idCard($str) {
        $str = trim($str);
        if (empty($str)) {
            return false;
        }

        if (preg_match("/^([0-9]{15}|[0-9]{17}[0-9a-z])$/i", $str)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 验证网址
     */
    public static function isURL($str) {
        if (empty($str)) {
            return false;
        }

        return preg_match('#(http|https|ftp|ftps)://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?#i',
                        $str) ? false : true;
    }

    /**
     * 验证邮箱
     */
    public static function isEmail($str) {
        if (empty($str)) {
            return false;
        }
        $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
        if (strpos($str, '@') !== false && strpos($str, '.') !== false) {
            if (preg_match($chars, $str)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

}
